package org.ccay.excel.importer.validate.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.ccay.core.log.ILogger;
import org.ccay.core.log.CcayLoggerFactory;
import org.ccay.core.util.StringUtil;
import org.ccay.excel.importer.validate.AbstractValidator;
import org.ccay.excel.importer.validate.IStatus;

/**
 * 
 * 日期范围验证  可以指定具体的value 或者title属性value
 * Created：2012-7-10
 * @author chaowangbang
 * @version:
 */
public class DateRangeValidator extends AbstractValidator {
	
	public static final String MESSAGE_KEY = "ccay.excel.validate.dateRange.error";
	
	public static final String MAXDATE_MESSAGE_KEY = "ccay.excel.validate.dateRange.maxDate.error";
	
	public static final String MINDATE_MESSAGE_KEY = "ccay.excel.validate.dateRange.minDate.error";
	
	public static final String MAXTITLE_MESSAGE_KEY = "ccay.excel.validate.dateRange.maxTitle.error";
	
	public static final String MINTITLE_MESSAGE_KEY = "ccay.excel.validate.dateRange.minTitle.error";
	
	
	//系统日期关键字
	public static final String SYSDATE = "Sysdate";
	
	//最大值
    private String maxDate;
    
    //最小值
    private String minDate;
    
    //比较最大的字段值
    private String maxTitle;
    
    //比较最小的字段值
    private String minTitle;
    
  //最大值
    private long maxDateValue;
    
    //最小值
    private long minDateValue;
    
    //比较最大的字段值
    private long maxTitleValue;
    
    //比较最小的字段值
    private long minTitleValue;
    
    /** 日期格式 **/
	private String format;
	
	private SimpleDateFormat dateFormat;
    
	private static ILogger logger = CcayLoggerFactory.getLogger(DateRangeValidator.class);	 
	
	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
		this.dateFormat = new SimpleDateFormat(this.format);
	}

	@Override
	protected IStatus doValidate(Object value) {
		if(null == value || StringUtil.isNullOrEmpty(value.toString())){
			return IStatus.Ok;
		}
		initParamValue();
		initTitleValue();
			long dateValue = 0;
			try {
				Date date = null;
				if(Date.class.isAssignableFrom(value.getClass())){
					 date  = (Date) value;
				}else{
					 date = this.dateFormat.parse(value.toString());
				}
				dateValue =  date.getTime();
			} catch (ParseException ex) {
				super.setMessageKey("ccay.excel.validate.date.error");
				return error();
			}
			try {
				return logicValidate(dateValue);
			} catch (ParseException ex) {
				//有列日期是错误的  那么当前列会验证 这里不做处理
				return IStatus.Ok;
			}
		
	
	}
	/**
	 * 
	 * @param dateValue
	 * @return
	 * @throws ParseException
	 * Created：2012-8-20
	 * @author chaowangbang
	 */
	private IStatus logicValidate(long dateValue) throws ParseException {
		IStatus istatus = logicVaueValidate(dateValue);
		if(IStatus.Ok.equals(istatus)){
			istatus = logicTitleValueValidate(dateValue);
		}
		return istatus;
	}
	/**
	 * 
	 * @param dateValue
	 * @return
	 * @throws ParseException
	 * Created：2012-8-20
	 * @author chaowangbang
	 */
	private IStatus logicVaueValidate(long dateValue) throws ParseException {
		if(maxDateValue>0 && dateValue - maxDateValue>0){
			super.setMessageKey(MAXDATE_MESSAGE_KEY);
			return error();
		}
		if(minDateValue >0 && dateValue - minDateValue<0){
			super.setMessageKey(MINDATE_MESSAGE_KEY);
			return error();
		}
		return IStatus.Ok;
	}
	/**
	 * 
	 * @param dateValue
	 * @return
	 * @throws ParseException
	 * Created：2012-8-20
	 * @author chaowangbang
	 */
	private IStatus logicTitleValueValidate(long dateValue) throws ParseException {
		
		if(maxTitleValue>0 && dateValue - maxTitleValue>0){
			super.setMessageKey(MAXTITLE_MESSAGE_KEY);
			return error();
		}
		if(minTitleValue>0 && dateValue - minTitleValue<0){
			super.setMessageKey(MINTITLE_MESSAGE_KEY);
			return error();
		}
		return IStatus.Ok;
	}
	/**
	 * 初始化日期参数value
	 * Created：2012-7-12
	 * @author chaowangbang
	 */
	private void initParamValue(){
		initMaxDateValue();
		initMinDateValue();
	} 
	/**
	 * 初始化最大的日期
	 * Created：2013-3-24
	 * @author chaowangbang
	 */
	private void initMaxDateValue(){
		try{
			if(!StringUtil.isNullOrEmpty(maxDate) && !SYSDATE.equalsIgnoreCase(maxDate)){
				maxDateValue = this.dateFormat.parse(maxDate).getTime();
			}else if(SYSDATE.equalsIgnoreCase(maxDate)){
				maxDateValue = getSystemDate();
			}
		}catch (ParseException ex) {
			logger.error("date format error!",ex);
		}
	}
	 /**
	  * 初始化最小的日期
	  * Created：2013-3-24
	  * @author chaowangbang
	  */
	private void initMinDateValue(){
		try{
			if(!StringUtil.isNullOrEmpty(minDate) && !SYSDATE.equalsIgnoreCase(minDate)){
				minDateValue = this.dateFormat.parse(minDate).getTime() ;
			}else if(SYSDATE.equalsIgnoreCase(minDate)){
				minDateValue = getSystemDate();
			}
		}catch (ParseException ex) {
			logger.error("date format error!",ex);
		}
	}
	/**
	 * 获取系统当前时间的long
	 * @return
	 * Created：2013-1-11
	 * @author chaowangbang
	 */
	private long getSystemDate(){
		try{
			return this.dateFormat.parse(this.dateFormat.format(new Date())).getTime();
		}catch (ParseException ex) {
			logger.error("date format error!",ex);
		}
		return new Date().getTime();
	}
	/**
	 * 初始化指定的titlevalue
	 * Created：2012-7-12
	 * @author chaowangbang
	 */
	private void initTitleValue(){
		maxTitleValue = getDateValue(maxTitle);
		minTitleValue = getDateValue(minTitle);
	}
	
	/**
	 * 获取对象格式的value
	 * @param title
	 * @return
	 */
	private long getDateValue(String title){
		Object value = super.getTitleObjectValue(title); 
		Date date = null;
		if(value == null || StringUtil.isNullOrEmpty(value.toString())){
			return 0;
		}
		 try{
			if(Date.class.isAssignableFrom(value.getClass())){
				 date  = (Date) value;
			}else{
				date = this.dateFormat.parse(value.toString());
			}
		} catch (ParseException ex) {
			logger.error("date format error!",ex);
			return 0;
		}	
		return date.getTime();
	}
	
	public String getMaxDate() {
		return maxDate;
	}

	public void setMaxDate(String maxDate) {
		this.maxDate = maxDate;
	}

	public String getMinDate() {
		return minDate;
	}

	public void setMinDate(String minDate) {
		this.minDate = minDate;
	}

	public String getMaxTitle() {
		return maxTitle;
	}

	public void setMaxTitle(String maxTitle) {
		this.maxTitle = maxTitle;
	}

	public String getMinTitle() {
		return minTitle;
	}

	public void setMinTitle(String minTitle) {
		this.minTitle = minTitle;
	}

	
	

}
